using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SQLite;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Sec_scan漏洞扫描系统
{
    public partial class Sec_Scan_LoopList : Form
    {
        public Sec_Scan_LoopList()
        {
            InitializeComponent();
        }

        private void LoopList_Load(object sender, EventArgs e)
        {
            //刷新漏洞
            ReferLoopInfo();
            dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
        }
        /// <summary>
        /// 刷新漏洞方法
        /// </summary>
        public void ReferLoopInfo()
        {
            this.dataGridView1.AutoGenerateColumns = true;
            this.source = new DataSet();
            this.pageSize = 24;
            this.currentPage = 1;
            string sql = Sec_Api.Sec_AllAPI.Sqlclient(Sec_Api.Sec_LoopScan_Service_Api.P1);
            SQLiteConnection connection = new SQLiteConnection(sql);
            try
            {
                connection.Open();
                if(connection.State == ConnectionState.Open)
                {
                    string sqls = Sec_Api.Sec_AllAPI.LoopInfos(Sec_Api.Sec_LoopScan_Service_Api.P1);
                    SQLiteDataAdapter adapter = new SQLiteDataAdapter(sqls, connection);
                    //adapter.Fill(source, "Column1");
                    adapter.Fill(source);
                    DataTable dtb = source.Tables[0];
                    dataGridView1.DataSource = dtb;
                    this.dataGridView1.Columns["Column1"].DataPropertyName = dtb.Columns["ID"].ToString();
                    this.dataGridView1.Columns["Column2"].DataPropertyName = dtb.Columns["sec_loopname"].ToString();
                    this.dataGridView1.Columns["Column3"].DataPropertyName = dtb.Columns["sec_looppayload"].ToString();
                    this.dataGridView1.Columns["Column4"].DataPropertyName = dtb.Columns["sec_looptype"].ToString();
                    this.dataGridView1.Columns["Column5"].DataPropertyName = dtb.Columns["sec_accuracy"].ToString();
                    this.dataGridView1.Columns["Column6"].DataPropertyName = dtb.Columns["sec_addtime"].ToString();

                    this.dataGridView1.Columns["ID"].Visible = false;
                    this.dataGridView1.Columns["sec_loopname"].Visible = false;
                    this.dataGridView1.Columns["sec_looppayload"].Visible = false;
                    this.dataGridView1.Columns["sec_looptype"].Visible = false;
                    this.dataGridView1.Columns["sec_accuracy"].Visible = false;
                    this.dataGridView1.Columns["sec_addtime"].Visible = false;
                    BindPage();
                    //int Dins = dataGridView1.Rows.Count;
                    //label8.Text = Dins.ToString();
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //dataGridView1.Rows.Clear();//刷新前先清除集合
            ReferLoopInfo();
            MessageBox.Show("刷新成功！");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //根据同的类型进行漏洞筛选
            SearchLoop();
        }
        /// <summary>
        /// 筛选漏洞
        /// </summary>
        public void SearchLoop()
        {
            this.dataGridView1.AutoGenerateColumns = true;
            this.source = new DataSet();
            this.pageSize = 24;
            this.currentPage = 1;
            string sql = Sec_Api.Sec_AllAPI.Sqlclient(Sec_Api.Sec_LoopScan_Service_Api.P1);
            SQLiteConnection connection = new SQLiteConnection(sql);
            try
            {
                connection.Open();
                if (connection.State == ConnectionState.Open)
                {
                    string sqls = "SELECT ID,sec_loopname ,sec_looppayload ,sec_looptype ,sec_accuracy ,sec_addtime  FROM Sec_LoopDetails WHERE sec_looptype='" + comboBox1.Text+"'";
                    if(comboBox1.Text == "所有类型")
                    {
                        //如果是该字段 直接终止
                        ReferLoopInfo();//刷新所有漏洞
                        return;
                    }
                    SQLiteDataAdapter adapter = new SQLiteDataAdapter(sqls, connection);
                    //adapter.Fill(source, "Column1");
                    adapter.Fill(source);
                    DataTable dtb = source.Tables[0];
                    dataGridView1.DataSource = dtb;
                    this.dataGridView1.Columns["Column1"].DataPropertyName = dtb.Columns["ID"].ToString();
                    this.dataGridView1.Columns["Column2"].DataPropertyName = dtb.Columns["sec_loopname"].ToString();
                    this.dataGridView1.Columns["Column3"].DataPropertyName = dtb.Columns["sec_looppayload"].ToString();
                    this.dataGridView1.Columns["Column4"].DataPropertyName = dtb.Columns["sec_looptype"].ToString();
                    this.dataGridView1.Columns["Column5"].DataPropertyName = dtb.Columns["sec_accuracy"].ToString();
                    this.dataGridView1.Columns["Column6"].DataPropertyName = dtb.Columns["sec_addtime"].ToString();

                    this.dataGridView1.Columns["ID"].Visible = false;
                    this.dataGridView1.Columns["sec_loopname"].Visible = false;
                    this.dataGridView1.Columns["sec_looppayload"].Visible = false;
                    this.dataGridView1.Columns["sec_looptype"].Visible = false;
                    this.dataGridView1.Columns["sec_accuracy"].Visible = false;
                    this.dataGridView1.Columns["sec_addtime"].Visible = false;
                    BindPage();
                    //int Dins = dataGridView1.Rows.Count;
                    //label8.Text = Dins.ToString();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //添加漏洞
            Sec_Scan_Addloop addloop = new Sec_Scan_Addloop();
            addloop.StartPosition = FormStartPosition.CenterScreen;
            addloop.Show();
        }
        DataSet source;//用于存储数据库中的查询出来的数据
        public int pageSize = 0;//每页条目数
        public int pageCount = 0;
        public int currentPage = 1;
        /// <summary>
        /// 绑定combobox中的页码
        /// </summary>
        private void BindPage()
        {
            this.label6.Text = PageCount.ToString();
            this.comboBox2.Items.Clear();
            for (int i = 1; i <= pageCount; i++)
            {
                this.comboBox2.Items.Add("第" + i + "页");
            }
            this.comboBox2.SelectedIndex = 0;
        }
        public int PageCount
        {
            get
            {
                if (source != null)
                {
                    pageCount = (int)Math.Ceiling(source.Tables[0].Rows.Count * 1.0 / pageSize);
                }
                return pageCount;
            }
        }
        /// <summary>
        /// 当前页
        /// </summary>
        public int CurrentPage
        {
            get { return currentPage; }
            set
            {
                if (value <= 1)
                {
                    value = 1;
                    this.label5.Enabled = false;
                    this.label2.Enabled = false;
                    this.label3.Enabled = true;
                    this.label4.Enabled = true;
                }
                else if (value >= PageCount)
                {
                    value = pageCount;
                    this.label5.Enabled = true;
                    this.label2.Enabled = true;
                    this.label3.Enabled = false;
                    this.label4.Enabled = false;
                }
                else
                {
                    this.label5.Enabled = true;
                    this.label2.Enabled = true;
                    this.label3.Enabled = true;
                    this.label4.Enabled = true;

                }
                currentPage = value;
            }
        }
        int currentRow;
        /// <summary>
        /// 每页首行索引
        /// </summary>
        public int CurrentRow
        {
            get
            {
                if (CurrentPage == 1)
                {
                    currentRow = 0;
                }
                else
                {
                    currentRow = (CurrentPage - 1) * pageSize;
                }
                return currentRow;
            }
        }
        int max = 0;
        /// <summary>
        /// 每页行数最大值索引
        /// </summary>
        public int Max
        {
            get
            {
                if (CurrentPage == pageCount)
                {
                    max = source.Tables[0].Rows.Count;
                }
                else
                {
                    max = pageSize * CurrentPage;
                }
                return max;
            }
        }
        /// <summary>
        /// 分页绑定数据
        /// </summary>
        private void BindSourceFY()
        {
            DataTable dt = new DataTable();
            dt.TableName = "tb";//这个可以没有
            //下面这个循环比较有用，没有这个循环直接绑定的话datagridview是不显示数据的
            //原因是在dt这个对象里面那个columns集合是空，所以这个datagridview没有办法自动绑定
            for (int i = 0; i < source.Tables[0].Columns.Count; i++)
            {
                dt.Columns.Add(source.Tables[0].Columns[i].ToString());
            }
            for (int i = CurrentRow; i < Max; i++)
            {
                dt.ImportRow(source.Tables[0].Rows[i]);
            }
            this.dataGridView1.DataSource = dt;
            //this.dgv.DataMember = "tb";

        }

        private void label5_Click(object sender, EventArgs e)
        {
            this.comboBox2.SelectedIndex = 0;
        }

        private void label2_Click(object sender, EventArgs e)
        {
            this.comboBox2.SelectedIndex = CurrentPage - 2;
        }

        private void label3_Click(object sender, EventArgs e)
        {
            this.comboBox2.SelectedIndex = CurrentPage;
        }

        private void label4_Click(object sender, EventArgs e)
        {
            this.comboBox2.SelectedIndex = PageCount - 1;
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            CurrentPage = this.comboBox2.SelectedIndex + 1;
            this.textBox1.Text = CurrentPage.ToString();
            BindSourceFY();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //验证漏洞
            Sec_Scan_VerifyLoop sec_Scan = new Sec_Scan_VerifyLoop();
            sec_Scan.StartPosition = FormStartPosition.CenterScreen;
            sec_Scan.ShowDialog();
        }
    }
}
